﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AuctionScanner.Properties;
using LuaInterface;

namespace AuctionScanner
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void selectAuctioneerluaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelectLuaFile();
            LoadLuaFile();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Settings.Default.AuctioneerFile))
            {
                SelectLuaFile();
            }
            LoadLuaFile();
        }

        private void SelectLuaFile()
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;

            Lua lua = new Lua();
            try
            {
                lua.DoFile(openFileDialog1.FileName);
                Settings.Default.AuctioneerFile = openFileDialog1.FileName;
                Settings.Default.Save();
            }
            catch (LuaException)
            {
                MessageBox.Show("Arquivo Lua inválido");
                SelectLuaFile();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Erro ao abrir arquivo: " + exc.Message);
            }
            finally
            {
                lua.Dispose();
            }
        }

        private void LoadLuaFile()
        {
            if (string.IsNullOrEmpty(Settings.Default.AuctioneerFile)) return;

            Lua lua = new Lua();

            lua.DoFile(Settings.Default.AuctioneerFile);

            LuaTable scans = lua.DoString(@"return AucScanData.scans.Warsong.Horde")[0] as LuaTable;

            string command = lua.DoString(@"return AucScanData.scans.Warsong.Horde.image")[0] as string;
            LuaTable image = lua.DoString(command)[0] as LuaTable;

            double id;
            LuaTable values;
            foreach (DictionaryEntry item in image)
            {
                id = Convert.ToDouble(item.Key);
                values = item.Value as LuaTable;
                double a = Convert.ToDouble(values[6]);
                auctioneerDataSet.AuctionItem.AddAuctionItemRow(id, Convert.ToString(values[1]),
                Convert.ToDouble(values[2]), Convert.ToString(values[3]), Convert.ToString(values[4]),
                Convert.ToDouble(values[5]), Convert.ToDouble(values[6]), Convert.ToDouble(values[7]),
                Convert.ToDouble(values[8]), Convert.ToString(values[9]), Convert.ToString(values[10]),
                Convert.ToDouble(values[11]), Convert.ToDouble(values[12]), Convert.ToDouble(values[13]),
                Convert.ToDouble(values[14]), Convert.ToDouble(values[15]), Convert.ToDouble(values[16]),
                Convert.ToDouble(values[17]), Convert.ToDouble(values[19]), Convert.ToDouble(values[19]),
                Convert.ToString(values[20]), Convert.ToDouble(values[21]), Convert.ToDouble(values[22]),
                Convert.ToDouble(values[23]), Convert.ToDouble(values[24]), Convert.ToDouble(values[25]),
                Convert.ToDouble(values[26]), Convert.ToDouble(values[27]));
            }

            auctioneerDataSet.AcceptChanges();
            itemBindingSource.Sort = "Name";
            lua.Dispose();

/*
            LuaTable scanDb = lua.GetTable("AucScanData");
            if (scanDb == null)
            {
                MessageBox.Show("Este não é um arquivo Auctioneer.lua válido");
                return;
            }


            LuaTable scans = scanDb["scans"] as LuaTable;
            LuaTable warsong = scans["Warsong"] as LuaTable;
            LuaTable horde = scans["Horde"] as LuaTable;
            //LuaTable image = horde["image"] as LuaTable;


            LuaTable tableItens = scanDb["items"] as LuaTable;
            LuaTable tableClasses = scanDb["auctionItemClasses"] as LuaTable;
            LuaTable tableSubclasses = scanDb["auctionItemSubClasses"] as LuaTable;


            foreach (DictionaryEntry cat in tableClasses)
            {
                auctioneerDataSet.Class.AddClassRow(Convert.ToInt32(cat.Key), cat.Value.ToString());
            }

            foreach (DictionaryEntry sub in tableSubclasses)
            {
                auctioneerDataSet.SubClass.AddSubClassRow(Convert.ToInt32(sub.Key), sub.Value.ToString());
            }

            string id;
            string[] decId;
            string[] itemData;
            int classId;
            int subId;
            foreach (DictionaryEntry item in tableItens)
            {
                id = item.Key.ToString();
                decId = id.Split(':');
                itemData = item.Value.ToString().Split(';');
                classId = Convert.ToInt32(itemData[3]);
                subId = Convert.ToInt32(itemData[4]);

                auctioneerDataSet.Item.AddItemRow(id, itemData[0], classId, subId, Convert.ToInt32(decId[0]),
                    Convert.ToInt32(decId[1]), Convert.ToInt32(decId[2]), auctioneerDataSet.Class.FindByClassId(classId).Name,
                    auctioneerDataSet.SubClass.FindBySubClassId(subId).Name);
            }

            auctioneerDataSet.AcceptChanges();

            itemBindingSource.Sort = "Name";

            lua.Dispose();
 * */
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            auctionItemBindingSource.Filter = string.Format("Name like '%{0}%'", textBox1.Text);
        }
    }
}
